package com.venutech.gongan.api;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.project.system.domain.SysUser;
import com.shihang.misc.date.DateTimeUtil;
import com.venutech.gongan.common.Constants;
import com.venutech.gongan.entity.BarCode;
import com.venutech.gongan.entity.Log;
import com.venutech.gongan.entity.QRCode;
import com.venutech.gongan.service.BarCodeService;
import com.venutech.gongan.service.QRCodeService;
import com.venutech.gongan.util.FileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * 一些公共的图片获取接口。
 * 二维码、条形码等，不判定权限
 */
@RestController
@RequestMapping("source/image")
@Anonymous
@Api(tags = "一些静态资源的获取接口")
public class ImageSourceController {
    @Autowired
    private QRCodeService qrCodeService;

    @Autowired
    private BarCodeService barCodeService;

    @Value("${qrcode.save-dir}")
    String qrCodeSaveDir;

    @Value("${barcode.save-dir}")
    String barCodeSaveDir;


    @ApiOperation("二维码展示")
    @GetMapping("qrcode/{id}")
    public void qrCodeImage(@PathVariable("id") String qrCodeId, HttpServletResponse response) {
        QRCode qrCode = qrCodeService.getById(Integer.parseInt(qrCodeId));
        //判断文件是否存在
        String filePath = String.format("%s%s", qrCodeSaveDir, qrCode.getQr_codeImgFileSaveDir());
        File files = new File(filePath);
        boolean flag = files.exists();
        if (flag) {
            try {
                //根据文件路径下载文件信息
                FileUtils.sendImage(response, filePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @ApiOperation("条型码展示")
    @GetMapping("barcode/{id}")
    public void barcodeImage(@PathVariable("id") Integer barCodeId, HttpServletResponse response) {
        BarCode barCode = barCodeService.getById(barCodeId);
        //判断文件是否存在
        String filePath = String.format("%s%s.png", barCodeSaveDir, barCode.getReal_code());
        File files = new File(filePath);
        boolean flag = files.exists();
        if (flag) {
            try {
                //根据文件路径下载文件信息
                FileUtils.sendImage(response, filePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
